package cn.softgame.jserver;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class SocketHelper {

	public void close(SocketChannel sc){
		try {
			if(sc!=null){
				sc.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public byte[] readSocket(SocketChannel sc) throws IOException {
		byte[] array = new byte[512];
		ByteBuffer buffer = ByteBuffer.wrap(array);
		if(sc!=null){
			sc.read(buffer);
		}
		return buffer.array();
	}

	public void writeSocket(byte[] bytes, SocketChannel sc) throws IOException {
		if(sc!=null){
			ByteBuffer src = ByteBuffer.allocate(bytes.length);
			src.put(bytes);
			sc.write(src);
		}
	}
}
